package cn.iocoder.yudao.module.train.controller.admin.classsetting;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.classsetting.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.classsetting.ClassSettingDO;
import cn.iocoder.yudao.module.train.service.classsetting.ClassSettingService;

@Tag(name = "管理后台 - 排课")
@RestController
@RequestMapping("/train/class-setting")
@Validated
public class ClassSettingController {

    @Resource
    private ClassSettingService classSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建排课")
    @PreAuthorize("@ss.hasPermission('train:class-setting:create')")
    public CommonResult<Long> createClassSetting(@Valid @RequestBody ClassSettingSaveReqVO createReqVO) {
        return success(classSettingService.createClassSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排课")
    @PreAuthorize("@ss.hasPermission('train:class-setting:update')")
    public CommonResult<Boolean> updateClassSetting(@Valid @RequestBody ClassSettingSaveReqVO updateReqVO) {
        classSettingService.updateClassSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排课")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:class-setting:delete')")
    public CommonResult<Boolean> deleteClassSetting(@RequestParam("id") Long id) {
        classSettingService.deleteClassSetting(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除排课")
                @PreAuthorize("@ss.hasPermission('train:class-setting:delete')")
    public CommonResult<Boolean> deleteClassSettingList(@RequestParam("ids") List<Long> ids) {
        classSettingService.deleteClassSettingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排课")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:class-setting:query')")
    public CommonResult<ClassSettingRespVO> getClassSetting(@RequestParam("id") Long id) {
        ClassSettingDO classSetting = classSettingService.getClassSetting(id);
        return success(BeanUtils.toBean(classSetting, ClassSettingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得排课分页")
    @PreAuthorize("@ss.hasPermission('train:class-setting:query')")
    public CommonResult<List<ClassSettingRespVO>> getClassSettingPage(@Valid ClassSettingPageReqVO pageReqVO) {
        List<ClassSettingDO> pageResult = classSettingService.getClassSettingList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassSettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排课 Excel")
    @PreAuthorize("@ss.hasPermission('train:class-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassSettingExcel(@Valid ClassSettingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassSettingDO> list = classSettingService.getClassSettingList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "排课.xls", "数据", ClassSettingRespVO.class,
                        BeanUtils.toBean(list, ClassSettingRespVO.class));
    }

}
